package com.shxc.smarttrade.base;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.shxc.smarttrade.MyApplication;
import com.shxc.smarttrade.dialog.LoadingDialog;
import com.shxc.smarttrade.utils.GlideUtils;
import com.shxc.smarttrade.utils.sp.SpImp;
import com.shxc.smarttrade.utils.ToastUtils;

import org.xchris.x;

/**
 * Fragmenet 基类
 */
public abstract class BaseFragment extends Fragment {

    private boolean injected = false;
    public SpImp spImp;
    public Bundle bundle;

    public LayoutInflater mInflater;
    public Resources resources;
    public LoadingDialog loadingDialog;
    public Context mContext;
    public MyApplication myApplication;
    public GlideUtils glideUtils;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        spImp = new SpImp(getActivity().getApplicationContext());
        bundle = new Bundle();
        mInflater = LayoutInflater.from(getActivity());
        resources = getActivity().getResources();
        mContext = x.app().getApplicationContext();
        myApplication = new MyApplication();
        initDialog();
        glideUtils = new GlideUtils(mContext);
    }

    private void initDialog() {
        loadingDialog = new LoadingDialog(getActivity(),
                getActivity().getResources().getIdentifier("MyDialog", "style", getActivity().getPackageName()));
        loadingDialog.setCanceledOnTouchOutside(false);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        injected = true;
        return x.view().inject(this, inflater, container);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!injected) {
            x.view().inject(this, this.getView());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        if (isSubmitDialog.isShowing()) {
//            isSubmitDialog.dismiss();
//        }
        if (loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

//    public void JumpTo(Class cla) {
//        Intent intent = new Intent(getActivity(), cla);
//        startActivity(intent);
//    }

    public void JumpTo(Class cla,Bundle b) {
        Intent intent = new Intent(getActivity(), cla);
        if (b!=null) {
            intent.putExtras(b);
        }
        startActivity(intent);
    }

}
